package com.df.bpm.service.impl;

import com.df.bpm.dto.InformatizationOutputDTO;
import com.df.bpm.dto.InformatizationRequirementBasicDTO;
import com.df.bpm.dto.InformatizationRequirementDetailsDTO;
import com.df.bpm.entity.InformatizationReason;
import com.df.bpm.entity.InformatizationTarget;
import com.df.bpm.mapper.InformatizationRequirementBasicDTOmapper;
import com.df.common.utils.MyQueryWrapper4Like;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.InformatizationRequirement;
import com.df.bpm.mapper.InformatizationRequirementMapper;
import com.df.bpm.service.InformatizationRequirementService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 申请单基础信息服务
 */
@Service
@RequiredArgsConstructor
public class InformatizationRequirementServiceImpl extends BaseServiceImpl<InformatizationRequirementMapper,InformatizationRequirement> implements InformatizationRequirementService {
	private final InformatizationRequirementMapper informatizationRequirementMapper;
	private final InformatizationRequirementBasicDTOmapper informatizationRequirementBasicDTOmapper;
	/**
	 * 需求原因服务
	 */
	private final InformatizationReasonServiceImpl informatizationReasonServiceImpl;
	/**
	 * 需求预期目标服务
	 */
	private final InformatizationTargetServiceImpl informatizationTargetServiceImpl;
	/**
	 * 需求预期输出服务
	 */
	private final InformatizationOutputServiceImpl informatizationOutputServiceImpl;
	/**
	 * 需求预期输出DTO服务
	 */
	private final InformatizationOutputDTOServiceImpl informatizationOutputDTOServiceImpl;



	/**
	 * 添加需求详情
	 * <P>原因、目标、预期输出物<P/>
	 * @param detailsDTO 需求详情（原因、目标、预期输出物）
	 * @return 成功添加的需求详情（原因、目标、预期输出物）
	 */
	public InformatizationRequirementDetailsDTO addDetailsDTO(InformatizationRequirementDetailsDTO detailsDTO){
		Integer requirementIndex=detailsDTO.getBasic().getInformatizationRequirementIndex();
		List<InformatizationReason> reasons = detailsDTO.getReasons();
		if(CommonUtils.varIsNotBlank(reasons)){
			List<InformatizationReason> reasonsOK=new ArrayList<>();
			for (InformatizationReason reason:reasons){
				if(reason.getInformatizationReasonIndex()==null) {
					reason.setInformatizationRequirementIndex(requirementIndex);
					if (informatizationReasonServiceImpl.add(reason) > 0) {
						reasonsOK.add(reason);
					}
				}else{
					reasonsOK.add(reason);
				}
			}
			if(!reasonsOK.isEmpty()){
				detailsDTO.setReasons(reasonsOK);
			}
		}
		List<InformatizationTarget> targets = detailsDTO.getTargets();
		if(CommonUtils.varIsNotBlank(targets)){
			List<InformatizationTarget> targetsOK=new ArrayList<>();
			for (InformatizationTarget target:targets){
				if(target.getInformatizationTargetIndex()==null){
					target.setInformatizationRequirementIndex(requirementIndex);
					if(informatizationTargetServiceImpl.add(target)>0) {
						targetsOK.add(target);
					}
				}else{
					targetsOK.add(target);
				}
			}
			if(!targetsOK.isEmpty()){
				detailsDTO.setTargets(targetsOK);
			}
		}
		List<InformatizationOutputDTO> outputs = detailsDTO.getOutputs();
		if(CommonUtils.varIsNotBlank(outputs)){
			List<InformatizationOutputDTO> outputsOK=new ArrayList<>();
			for (InformatizationOutputDTO output:outputs){
				if(output.getInformatizationOutputIndex()==null) {
					output.setInformatizationRequirementIndex(requirementIndex);
					if (informatizationOutputServiceImpl.add(output) > 0) {
						outputsOK.add(output);
					}
				}else{
					outputsOK.add(output);
				}
			}
			if(!outputsOK.isEmpty()){
				detailsDTO.setOutputs(outputsOK);
			}
		}
		return detailsDTO;
	}

	public List<InformatizationRequirementBasicDTO> getList4Like(InformatizationRequirementBasicDTO temp) {
		if(CommonUtils.varIsBlank(temp)){
			return null;
		}
		MyQueryWrapper4Like<InformatizationRequirementBasicDTO> wrapper=new MyQueryWrapper4Like<>(temp);
		wrapper=wrapper.stringLike(temp);
		wrapper.orderByAsc("completionDateEstimate");
		return informatizationRequirementBasicDTOmapper.selectList(wrapper);
	}

    public InformatizationRequirementDetailsDTO getDetailsDto(Integer informatizationRequirementIndex) {
		InformatizationRequirementDetailsDTO result=new InformatizationRequirementDetailsDTO();
		InformatizationRequirement basic=this.getById(informatizationRequirementIndex);
		result.setBasic(basic);
		List<InformatizationReason> reasons=informatizationReasonServiceImpl.getList(
				new InformatizationReason().setInformatizationRequirementIndex(
						informatizationRequirementIndex));
		result.setReasons(reasons);
		List<InformatizationTarget> targets=informatizationTargetServiceImpl.getList(
				new InformatizationTarget().setInformatizationRequirementIndex(
						informatizationRequirementIndex));
		result.setTargets(targets);
		InformatizationOutputDTO temp=new InformatizationOutputDTO();
		temp.setInformatizationRequirementIndex(informatizationRequirementIndex);
		List<InformatizationOutputDTO> outputs=informatizationOutputDTOServiceImpl.getList(temp);
		result.setOutputs(outputs);
		return result;
	}

	public void saveRequirementDetailsDto(InformatizationRequirementDetailsDTO detailsDTO) {
		Integer requirementId=detailsDTO.getBasic().getInformatizationRequirementIndex();
		if(CommonUtils.varIsBlank(requirementId)){
			addDetailsDTO(detailsDTO);
		}else {
			informatizationRequirementMapper.updateById(detailsDTO.getBasic());
			informatizationReasonServiceImpl.saveReasons4RequirementId(requirementId,detailsDTO.getReasons());
			informatizationTargetServiceImpl.saveTargets4RequirementId(requirementId,detailsDTO.getTargets());
			informatizationOutputServiceImpl.saveOutputs4RequirementId(requirementId,detailsDTO.getOutputs());
		}
	}
}